package com.nuatar.meibi;

import java.util.ArrayList;
import java.util.List;

import com.nuatar.meibi.pubvar.ContactBoy;
import com.nuatar.meibi.pubvar.ContactMan;
import com.nuatar.meibi.pubvar.LAdapter;
import com.nuatar.meibi.pubvar.LAdapter.ViewBinder;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ShareActivity extends FatherActivity {
	String PhoneStr="";
	int ManNum=0;
	String Manstr="";
	TextView Title;
	EditText ManTag;
	LAdapter la;
	Button btn;
	static Handler mHanler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share);
		btn=(Button) findViewById(R.id.ShareBtn);
		btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO 分享按钮
				String content=getResources().getString(R.string.SugInfo);
				String[] PhoneNumArray=PhoneStr.substring(0, PhoneStr.length()-1).split(",");
				for(String a:PhoneNumArray){
					sendMessage(a,content,getApplicationContext());
				}
				SharedPreferences pre = PreferenceManager
						.getDefaultSharedPreferences(getApplicationContext());
				int coins=pre.getInt("MyCoins", 0);
				SharedPreferences.Editor edt = pre.edit();
				edt.putInt("MyCoins", coins+PhoneNumArray.length);
				edt.commit();
				Toast.makeText(getApplicationContext(), "积分已经增加"+coins, Toast.LENGTH_SHORT).show();
				finish();
			}});
		ListView lv=(ListView) findViewById(R.id.MsgList);
		Title=(TextView) findViewById(R.id.MsgText);
		ManTag=(EditText) findViewById(R.id.ManText);
		ViewBinder vb =new ViewBinder(){

			@Override
			public View getView(int position, Context ctx, Object data) {
				// TODO Auto-generated method stub
				ContactMan cm=new ContactMan(ctx,(ContactBoy)data);
				return cm;
			}

			@Override
			public View update(View v, int position, Object data) {
				// TODO Auto-generated method stub
				return v;
			}};
		la=new LAdapter(this.getApplicationContext(),null,vb);
		lv.setAdapter(la);
		lv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				boolean t= ((ContactMan)view).Tigger();
				if(t){
					PhoneStr+=((ContactMan)view).cb.PhoneNum;
					Manstr+=((ContactMan)view).cb.name+",";
					ManNum+=1;
				}else{
					ManNum-=1;
					PhoneStr=PhoneStr.replaceAll(((ContactMan)view).cb.PhoneNum, "");
					Manstr=Manstr.replaceAll(((ContactMan)view).cb.name+",", "");
				}
				int NumAr=0;
				if(!Manstr.equals("")){
					NumAr=PhoneStr.substring(0, PhoneStr.length()-1).split(",").length;
					Manstr.substring(0, Manstr.length()-1);
				}
				Title.setText("当前可以增加"+NumAr+"个积分");
				ManTag.setText(Manstr);
			}});
		mHanler =new Handler(){
			@Override
			 public void handleMessage(Message msg) {
				la.notifyDataSetChanged();
		    }
		};
		Mt mt=new Mt();
		new Thread(mt).start();
		
	}

	public List<ContactBoy> getContact()
	 {    
		List<ContactBoy> result=null;
		Cursor cursor = getContentResolver().query(ContactsContract.
				 
				Contacts.CONTENT_URI,null, null, null, null);
		if(cursor.getCount()>0){
			result=new ArrayList<ContactBoy>();
			while(cursor.moveToNext()){
				
				ContactBoy temp=new ContactBoy();
				String id = cursor.getString(cursor.getColumnIndex(
				 
				BaseColumns._ID));
				String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				temp.name=name;
				int isHas = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
				 
				if(isHas>0){
				Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = " + id,null,null);
				String number="";
				while(c.moveToNext()){
				number += c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+",";
				
				}
				temp.PhoneNum=number;
				c.close();
				}
				result.add(temp);
				}
			
				cursor.close();
		}
			return result;	

	 }
	@Override
	protected void onStart() {
		super.onStart();
		mGaTracker.sendView(this.toString());
	}
	public void sendMessage(String num,String a,Context context){  
		SmsManager smsManager = SmsManager.getDefault();  
		List<String> divideContents = smsManager.divideMessage(a); 
		smsManager.sendMultipartTextMessage(num, null, (ArrayList<String>) divideContents, null, null);
	}
	class Mt implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			la.mlist=getContact();
			ShareActivity.mHanler.sendEmptyMessage(1);
		}}
}
